package com.jeecg.study.filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
 * @see 自定义过滤器，解决 post 请求乱码
 * @author wuhn
 * @date 2018年4月26日 14:12:44
 */
public class MyFilters implements Filter{
	
	private String encoding; //编码格式
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		System.out.println("init running .."+MyFilters.class.getName() +" ...");
		encoding=filterConfig.getInitParameter("encoding");
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		if(encoding !=null && encoding != ""){
			request.setCharacterEncoding(encoding);
			response.setCharacterEncoding(encoding);
		}
		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {
		
	}

}
